package com.h3bpm.kumhosunny.crm.controller.businessopportunity;

import OThinker.H3.Controller.ControllerBase;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @authormi
 * @date2019/10/10
 */
@Controller
@RequestMapping("/Portal/CRM/BusinessOpportunity")
public class BusinessOpportunityContrller extends ControllerBase {
    @Override
    public String getFunctionCode() {
        return null;
    }
    private  final String ONE="1";

    private  final String TOW="2";

    @RequestMapping("/getBoList")
    @ResponseBody
    public JSONObject getBoList(String clientData,String state,String start ){

        JSONObject result= new JSONObject();
        try {
      String userId =  getUserValidator().getUser().getObjectID();
      String parentId =  getUserValidator().getUser().getManagerID();

        String sql= " select * from i_businessopportunity a  where 1=1 ";

        if(clientData != null &&  !"".equals(clientData)){
          sql= " select * from i_businessopportunity a where  a.businessOpportunityName like '%"+clientData+"%' or  a.customerName like '%"+clientData+"%' or a.customerCode like '%"+clientData+"%'";
        }


        if(ONE.equals(state)){
            sql+=" and a.CreatedBy = '"+userId+"'";
        }
        if(TOW.equals(state)){
            sql+=" and a.CreatedByParentId = '"+parentId+"'";
        }
            sql+="    order by  a.CreatedTime desc  ";

            sql+="    limit  "+start+",10 ";

            DataTable table = SqlUtil.doQuery(sql);
        List<Map<String ,String >> list =new ArrayList<Map<String ,String > >();
        if(table!=null && table.getEntityRows()!=null && table.getEntityRows().size()>0){
            for( DataRow dataRow :  table.getEntityRows()){
                Map<String ,String > customerMap = new HashMap<String ,String >(3);
                String boName = dataRow.getString("businessOpportunityName");
                String customerName = dataRow.getString("customerName");
                String customerCode = dataRow.getString("customerCode");
                String objectId = dataRow.getString("ObjectID");
                String  mobile = dataRow.getString("mobile");
                customerMap.put("businessOpportunityName",boName);
                customerMap.put("customerName",customerName);
                customerMap.put("customerCode",customerCode);
                customerMap.put("mobile",mobile);
                customerMap.put("objectId",objectId);
                list.add(customerMap);
            }

        }

            result.put("data",list);
            result.put("status","200");
        } catch (Exception e) {
            result.put("status","500");
            e.printStackTrace();
        }


        return result;

    }



}
